//
//  LoginRegViewModel.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/6/30.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "LoginRegViewModel.h"
#import "LoginRegModel.h"

@implementation LoginRegViewModel

//短信验证码
- (RACSignal *)sendvaliRequestParameter:(NSString *) phone
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"phoneNumber"  :phone};
        
       [RequestOperationManager codeWithParametersDic:param success:^(NSDictionary *result) {
           
           
           NSLog(@"验证码。%@",result);
           
           [subscriber sendNext:result];
           [subscriber sendCompleted];
           
       } failture:^(id result) {
           [subscriber sendError:result];
       }];
        
        return nil;
    }];
}
//注册
- (RACSignal *)RegisterRequestParameter:(NSString *) phone password:(NSString *) pwd userName:(NSString *) name code:(NSString * )code strTime:(NSString *) time{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        
        
        NSDictionary *param = @{@"phoneNumber"  :phone,
                                @"username"  :name,
                                @"password":pwd,
                                @"code"  :code,
                                @"strTime":time};
        
        
        [RequestOperationManager registerWithParametersDic:param success:^(NSDictionary *result){
            
            NSLog(@"注册的信息 %@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
            
        } failture:^(id result) {
            
            
            [subscriber sendError:result];
        }];
        return nil;
    }];
}
//登录
-(RACSignal *)LoginRequestParameter:(NSString *)loginPhone pwd:(NSString *)loginPwd{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"phoneNumber"  :loginPhone,
                                @"password"      :loginPwd};
        
       
        [RequestOperationManager LoginWithParametersDic:param success:^(NSDictionary *result) {

            
            [DEF_UserDefaults setObject:loginPhone forKey:DEF_USERDEFAULTS_USERNAME];
            [DEF_UserDefaults setObject:loginPwd forKey:DEF_USERDEFAULTS_USERPASSWORD];
            [DEF_UserDefaults synchronize];
            
            
            UserInfoShare.isLogin = YES;
            
            
            UserInfoShare.userCenterModel = [MTLJSONAdapter modelOfClass:[LoginRegModel class] fromJSONDictionary:result[@"result"] error:nil];
            
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            
            [subscriber sendError:result];
        }];
        return nil;
    }];
}

-(RACSignal *)LoginOutRequestParameter:(NSString *)loginPhone{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"userId"  :loginPhone};
        
        [RequestOperationManager LoginOutWithParametersDic:param success:^(NSDictionary *result) {
            
            
            [DEF_UserDefaults removeObjectForKey:DEF_USERDEFAULTS_USERNAME];
            [DEF_UserDefaults removeObjectForKey:DEF_USERDEFAULTS_USERPASSWORD];
            [DEF_UserDefaults synchronize];
            UserInfoShare.isLogin = NO;
            
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            [subscriber sendError:result];
        }];
        
        return nil;
    }];
    
}
@end
